package com.thinking.dubbo.api;

import com.thinking.dubbo.common.TtlThreadUtil;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName I18nMessageUtil
 * @Description TODO
 * @Author tangsixiang@gmail.com
 * @Date 2023/6/5 8:56
 * @Version 1.0
 */


public class I18nMessageUtil {

    private static Map<String,MessageSourceAccessor> messageSourceAccessorMap =new ConcurrentHashMap<>();
    private static MessageSourceAccessor accessor;
    private static final String PATH_PARENT = "classpath:i18n/";
    private static final String SUFFIX = ".properties";
    private static ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    private I18nMessageUtil(){
    }
//    /** * 初始化资源文件的存储器 * 加载指定语言配置文件 * * @param language 语言类型(文件名即为语言类型,eg: en_us 表明使用 美式英文 语言配置) */
//    private static MessageSourceAccessor initMessageSourceAccessor(String language) throws IOException {
//        /** * 获取配置文件名 */
//        Resource resource = resourcePatternResolver.getResource(PATH_PARENT+"messages_" + language + SUFFIX);
//        String fileName = resource.getURL().toString();
//        int lastIndex = fileName.lastIndexOf(".");
//        String baseName = fileName.substring(0,lastIndex);
//        /** * 读取配置文件 */
//        ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource = new ReloadableResourceBundleMessageSource();
//        reloadableResourceBundleMessageSource.setBasename(baseName);
//        reloadableResourceBundleMessageSource.setCacheSeconds(5);
//        reloadableResourceBundleMessageSource.setDefaultEncoding("UTF-8");
//        accessor = new MessageSourceAccessor(reloadableResourceBundleMessageSource);
//        return  accessor;
//    }

    /**
     * 通过语言、提示信息  默认提示语
     */
     public static String getMessage(String language, String message, String defaultMessage) throws IOException {
        if(messageSourceAccessorMap.containsKey(language)){
            return  messageSourceAccessorMap.get(language).getMessage(message,defaultMessage);
        }
        return  messageSourceAccessorMap.get("default").getMessage(message,defaultMessage);
    }


    /***
     *  通过全局提示码获取响应提示信息 指定语言
     * @param language
     * @param code
     * @return
     * @throws IOException
     */
    public static String getMessage(String language, Integer  code) throws IOException {
            if(messageSourceAccessorMap.containsKey(language)){
                return  messageSourceAccessorMap.get(language).getMessage(String.valueOf(code));
            }
          return  messageSourceAccessorMap.get("default").getMessage(String.valueOf(code));
    }

    /**
     * 通过全局提示码获取响应提示信息  获取默认语言
     * @param code
     * @return
     * @throws IOException
     */
    public static String getMessage( Integer code) throws IOException {
        String language = TtlThreadUtil.getThreadLocal().get();

        if(language!=null&&messageSourceAccessorMap.containsKey(language)){
            return  messageSourceAccessorMap.get(language).getMessage(String.valueOf(code));
        }
        return  messageSourceAccessorMap.get("default").getMessage(String.valueOf(code));
    }

    public static  void messageSourceAccessor(String language) throws IOException {
        /** * 获取配置文件名 */
        String messages="messages_"+language;
        if(language.equals("default")) {
            messages="messages";
        }
        Resource resource = resourcePatternResolver.getResource(PATH_PARENT+messages+ SUFFIX);
        String fileName = resource.getURL().toString();
        int lastIndex = fileName.lastIndexOf(".");
        String baseName = fileName.substring(0,lastIndex);
        /** * 读取配置文件 */
        ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource = new ReloadableResourceBundleMessageSource();
        reloadableResourceBundleMessageSource.setBasename(baseName);
        reloadableResourceBundleMessageSource.setCacheSeconds(5);
        reloadableResourceBundleMessageSource.setDefaultEncoding("UTF-8");
        messageSourceAccessorMap.put(language, new MessageSourceAccessor(reloadableResourceBundleMessageSource));
    }


}
